package com.lee.controller;

import com.lee.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */

@Controller
@RequestMapping("index")
public class IndexController {
    private int count = 0;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RedisOperationsSessionRepository repository;
    @RequestMapping("list")
    @ResponseBody
    public Result list(HttpSession session){

        session.setAttribute("name","leegean");
        return Result.ok("操作成功");

    }

    @RequestMapping(value="logout",produces = "application/json; charset=UTF-8")
    @ResponseBody
    public Result logout(HttpSession session){
        session.invalidate();
        return Result.ok("退出成功");
    }


    @RequestMapping(value = "login", produces = "application/json; charset=UTF-8")
    @ResponseBody
    public Result login(HttpSession session){

        System.out.println(session.getId());
        session.setAttribute("name","userId");

        List list = redisTemplate.opsForList().range("userId", 0, -1);
        for (Object o : list) {
            repository.delete(o.toString());

        }
        redisTemplate.opsForList().rightPush("userId", session.getId());
        return Result.ok("登录成功");
    }
}
